/*
 * 文件名：ClockMenu.js
 * 描述：蓄力收纳袋打开菜单
 * 日期: 2025/4/17
 * 参考文档：https://learn.microsoft.com/zh-cn/minecraft/creator/scriptapi/
 */

import * as mc from "@minecraft/server";
import * as ui from "@minecraft/server-ui";

/* 测试代码
function mainTick() {
  if (mc.system.currentTick % 1000 === 0) {
    //mc.world.sendMessage("Hello starter! Tick: " + Object.entries(mc));
	for (let key in mc.world.beforeEvents) {
		console.warn("K:"+key+" V:"+ mc.world.beforeEvents[key]);
	}
  }
  mc.system.run(mainTick);
}
mainTick();
*/

function strKV(o) {
	let str = "{";
	for (let key in o) {
		str += ("K:"+key+" V:"+ o[key]);
	}
	str += "}";
	return str;
}

// 格式化小数位数输出
function fixV3(v3) {
	return "(" + v3.x.toFixed(2) + "," + v3.y.toFixed(2) + "," + v3.z.toFixed(2) + ")";
}
function fixB3(b3) {
	return "(" + b3.x + "," + b3.y + "," + b3.z + ")";
}

function BlkInfo(b) {
	let a = {};
	a.tags = b.getTags();
	a.states = b.permutation.getAllStates();
	return JSON.stringify(a);
}
// 获取方块对应的物品名称
function blkid(b) {
	let i = b.getItemStack();
	if (i != null) {
		return i.typeId;
	}
	return BlkInfo(b);
}


// 玩家重生监听
mc.world.afterEvents.playerSpawn.subscribe((e)=>{
	let  p = e.player;
	p.getComponents().forEach((c)=>{
		console.log(c.typeId);
	})
});

var GB2312UnicodeConverter = {
ToUnicode: function(str) {
return escape(str).toLocaleLowerCase().replace(/%u/gi, '\\u');
},
ToGB2312: function(str) {
return unescape(str.replace(/\\u/gi, '%u'));
}
};

mc.system.afterEvents.scriptEventReceive.subscribe((e)=>{
	let id = e.id;
	let msg = e.message;
	if (e.id == "sysmsg:msg") {
		msg = GB2312UnicodeConverter.ToGB2312(e.message);
		let d = JSON.parse(msg);
		console.log("[testProperty] msg=" + d.msg);
	}
});

console.log("[testProperty] 属性测试已启动。");
